<?php

namespace Bundle\ModuleBundle\Plugin;

use Bundle\ModuleBundle\Module\Information;

use Bundle\ModuleBundle\Interfaces\PluginInterface;
use Bundle\ModuleBundle\Interfaces\ModuleInterface;

use Bundle\ConfigurationBundle\Configuration;
/**
 * Базовый класс для плагина. 
 *
 */
abstract class Plugin implements PluginInterface, \IteratorAggregate
{
	/**
	 * Родительский модуль
	 * @var \Bundle\ModuleBundle\Interfaces\ModuleInterface
	 */
    protected $_module;
    protected $_configuration;

    public $name = '';
    public $panel = '';
    
    
    /**
     * 
     * @param \Bundle\ModuleBundle\Module $module
     * @param \Bundle\ConfigurationBundle\Configuration $configuration
     */
    public function __construct(ModuleInterface $module, Configuration $configuration)
    {
        $this->_module = $module;
        $this->_configuration = $configuration;
    }

    public function getConfiguration()
    {
        return $this->_configuration;
    }

    // @TODO: надо переписать???
    public function __call($name, $arguments)
    {
        if(0 == \strncmp($name, 'get', 3))
        {
            $field = \substr($name, 3);
            foreach($this->getFields() as $key => $value)
            {
                if(\strcasecmp($key, $field) === 0)
                {
                    return $value->getValue();
                }
            }
            throw new \Exception("Field `$field` wasn't found");
        }
        else if(0 == \strncmp($name, 'set', 3))
        {
            $field = \substr($name, 3);
            foreach($this->getFields() as $key => $value)
            {
                if(\strcasecmp($key, $field) === 0)
                {
                    $value->setValue($arguments[0]);
                    return;
                }
            }
            throw new \Exception("Field `$field` wasn't found");
        }
        else
        {
            throw new \Exception("Method `$name` can't be invoked");
        }
    }

    public function setRawData($data)
    {
        $fields = $this->getPersistantFields();

        foreach($data as $key => $value)
        {
            if(\array_key_exists($key, $fields))
            {
                $fields[$key]->setValue($value);
            }
        }
    }

    public function getPersistantFields()
    {
        if($this->_configuration !== null && isset($this->_configuration->database) &&
                array_key_exists('fields', $this->_configuration->database))
        {
            return $this->_configuration->database['fields'];
        }
        return array();
    }

    public function getFields()
    {
        if($this->_configuration !== null && isset($this->_configuration->fields))
        {
            return $this->_configuration->fields;
        }
        return array();
    }

    public function processEntityData($data)
    {
        $this->setRawData($data);
    }
    
    public function getIterator() 
    {
        return new \ArrayIterator($this->getFields());
    }
    
    /**
     * Подготовка значений
     */
    public function processFieldsData()
    {}
    
    public function getFieldValue($field, $datarow)
    {
    	return $datarow->{$field->name};
    }
}
